/*
 * Copyright 2012 Finbudget.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package finbudgettable.client.ui.grid.cell.currency;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.user.cellview.client.Header;

import finbudget.client.ui.table.Record;
import finbudgettable.client.ui.grid.CellGrid;
import finbudgettable.client.ui.grid.cell.CommonComparator;
import finbudgettable.client.ui.grid.cell.ContextMenu;
import finbudgettable.client.ui.grid.column.ColumnMenu;
import finbudgettable.client.ui.grid.column.ColumnMenuImpl;
import finbudgettable.client.ui.grid.column.GridColumn;

import java.math.BigDecimal;
import java.util.Comparator;

public class CurrencyColumn extends GridColumn<BigDecimal> {
	private final CellGrid grid;

    public CurrencyColumn(Builder builder) {
        super(builder);
        this.grid = builder.getCellGrid();
        initFieldUpdater(builder.getFieldName());
    }

    @Override
    protected boolean isValueChanged(Record record, BigDecimal value) {
        return CommonComparator.isNotEqual(getValue(record), value);
    }

    @Override
    protected String getCellStyleNames() {
        return CurrencyCell.RESOURCES.style().bCurrencyCell();
    }

    private void initFieldUpdater(final String fieldName) {
        setFieldUpdater(new FieldUpdater<Record, BigDecimal>() {
            public void update(int index, Record record, BigDecimal value) {
                record.setValue(fieldName, value != null ? value.toPlainString() : null);
                grid.redrawFooters();
            }
        });
    }

    @Override
    public BigDecimal getValue(Record record) {
        String value = record.getValueString(getFieldName());
        try {
        	return value != null ? new BigDecimal(value) : null;
        } catch (Exception e) {
        }
        return null;
    }

    public static class Builder extends GridColumn.Builder<CurrencyCell, BigDecimal> {
    	private CurrencyCell cell;

        Builder(CellGrid grid, String fieldName, boolean hasTotals) {
            super(grid, fieldName, hasTotals);
        }

        @Override
        protected CurrencyCell createCell() {
            return cell == null ? cell = new CurrencyCell(nullable) : cell;
        }

        @Override
        protected GridColumn<BigDecimal> createColumn(Cell<BigDecimal> cell) {
            return new CurrencyColumn(this);
        }

        @Override
        protected Comparator<Record> getDefaultComparator() {
            return new CurrencyComparator(fieldName);
        }

        public ColumnMenu getDefaultColumnMenu() {
            final ColumnMenu columnMenu = new ColumnMenuImpl(grid, new CurrencyFilterPanel(fieldName));
            setColumnMenu(columnMenu);
            return columnMenu;
        }

        @Override
        protected ContextMenu<BigDecimal> getDefaultContextMenu() {
            return new CurrencyContextMenu(grid, fieldName);
        }

        @Override
        protected Header<?> getFooter() {
        	return new CurrencyFooter(grid, fieldName);
        }

        protected CellGrid getCellGrid() {
        	return grid;
        }
    }

    public static Builder builder(CellGrid grid, String fieldName, boolean hasTotals) {
        return new Builder(grid, fieldName, hasTotals);
    }
}
